//
// Contributed by TheBrodieMan
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CommonBehaviors.Actions;
using System.Windows.Forms;
using System.Windows.Media;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Profiles;
using Styx.Pathing;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

using Action = Styx.TreeSharp.Action;

namespace Honorbuddy.Quest_Behaviors.SpecificQuests.ASquadOfYourOwn
{
	[CustomBehaviorFileName(@"13624-ASquadOfYourOwn")]
	public class ASquadOfYourOwn : CustomForcedBehavior
	{
		public ASquadOfYourOwn(Dictionary<string, string> args)
			: base(args)
		{
			try
			{
				QuestId = 13624; //GetAttributeAsQuestId("QuestId", true, null) ?? 0;
			}
			catch
			{
				Logging.Write("Problem parsing a QuestId in behavior: A Squad Of Your Own");
			}
		}
		
		public int QuestId { get; set; }
		private bool _isBehaviorDone;
		public int HordeInvaderId = 33334;
		public int WreckerId = 33283;
		public WoWPoint StartLocation = new WoWPoint(3240.626, 161.4901, 8.654869);
		private Composite _root;
		public QuestCompleteRequirement questCompleteRequirement = QuestCompleteRequirement.NotComplete;
		public QuestInLogRequirement questInLogRequirement = QuestInLogRequirement.InLog;
		public override bool IsDone
		{
			get
			{
				return _isBehaviorDone;
			}
		}
		public static void BPdiag(string message, params object[] args) { Logging.WriteDiagnostic(Colors.LawnGreen, "[QB DIAG TBM: " + message, args); }
		
		private LocalPlayer Me
		{
			get { return (StyxWoW.Me); }
		}
		
		public override void OnStart()
		{
			OnStart_HandleAttributeProblem();
		}
		
		public List<WoWUnit> Invader
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWUnit>().Where
					(u => u.Entry == HordeInvaderId && u.IsAlive).
					OrderBy(u => u.Distance).ToList();
			}
		}
		
		public List<WoWUnit> Wrecker
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWUnit>().Where
					(u => u.Entry == WreckerId && u.IsAlive).
					OrderBy(u => u.Distance).ToList();
			}
		}
		
		public bool IsQuestComplete()
		{
			var quest = StyxWoW.Me.QuestLog.GetQuestById((uint)QuestId);
			return quest == null || quest.IsCompleted;
		}
		
		private bool IsObjectiveComplete(int objectiveId, uint questId)
		{
			if (Me.QuestLog.GetQuestById(questId) == null)
			{
				return false;
			}
			int returnVal = Lua.GetReturnVal<int>("return GetQuestLogIndexByID(" + questId + ")", 0);
			return
				Lua.GetReturnVal<bool>(
					string.Concat(new object[] { "return GetQuestLogLeaderBoard(", objectiveId, ",", returnVal, ")" }), 2);
		}

		public Composite DoneYet
		{
			get
			{
				return
					new Decorator(ret => IsQuestComplete(), new Action(delegate
					{
						TreeRoot.StatusText = "Finished!";
						_isBehaviorDone = true;
						return RunStatus.Success;
					}));
			}
		}

		public Composite KillThem
		{
			get
			{
				return
					new Decorator(ret => Me.HasAura(62805), new Action(c =>
					{
						TreeRoot.StatusText = "Moving to Attack";
						var hostile =
							ObjectManager.GetObjectsOfType<WoWUnit>().Where(r => 
								r.GotTarget && r.CurrentTarget == Me).OrderBy(r=>
								r.Distance).FirstOrDefault();
						WoWUnit tar;
						if (hostile != null)
						{
							tar = hostile;
						}
						else if (Invader.Count > 0 && !IsObjectiveComplete(2,(uint)QuestId))
						{
							tar = Invader.FirstOrDefault();
						}
						else if (Wrecker.Count > 0 && !IsObjectiveComplete(1,(uint)QuestId))
						{
							tar = Wrecker.FirstOrDefault();
						}
						else
						{
							Logging.Write("No viable targets, waiting.");
							Navigator.MoveTo(StartLocation);
							return RunStatus.Failure;
						}

						if (Me.Location.Distance(tar.Location) > 15)
						{
							WoWMovement.ClickToMove(tar.Location);
						}
						else
						{
							tar.Target();
							tar.Face();
							Lua.DoString("StartAttack()");
						}
						return RunStatus.Failure;
					}));
			}
		}

		protected override Composite CreateBehavior()
		{
			return _root ?? (_root = new Decorator(ret => !_isBehaviorDone,
				new PrioritySelector(
					new Decorator(context => !Me.HasAura(62805),
						new Action(context => { _isBehaviorDone = true; })),
					DoneYet, KillThem, new ActionAlwaysSucceed())));
		}
	}
}